package alva.com.getcamera.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import alva.com.getcamera.R;
import alva.com.getcamera.bean.VideoBean;

/**
 * Created by Administrator on 2016/11/11 0011.
 */
public class VideoRecyclerViewAdapter extends RecyclerView.Adapter<VideoRecyclerViewAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {
    private Context context;
    private ArrayList<VideoBean> videoList;
    private RecyclerView mRecyclerView;
    private OnChildClickListener onChildClickListener;
    private OnChildLongClickListener onChildLongClickListener;

    public VideoRecyclerViewAdapter(Context context, ArrayList<VideoBean> videoList) {
        this.context = context;
        this.videoList = videoList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.video_recyclerview_item, parent, false);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        VideoBean videoBean=videoList.get(position);
        holder.videoImage.setImageBitmap(videoBean.getVideoBitmap());
        holder.videoName.setText(videoBean.getVideoName());
        holder.videoTime.setText(videoBean.getLastModified());
    }

    @Override
    public int getItemCount() {
        return videoList==null?0:videoList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        mRecyclerView=recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        mRecyclerView=null;
    }

    @Override
    public void onClick(View view) {
        if (onChildClickListener!=null){
            int position=mRecyclerView.getChildAdapterPosition(view);
            String path = videoList.get(position).getPath();
            onChildClickListener.onChildClick(mRecyclerView,view,position,getItemId(position),path);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (onChildLongClickListener!=null){
            int position=mRecyclerView.getChildAdapterPosition(v);
            String path = videoList.get(position).getPath();
            onChildLongClickListener.onChildLongClick(mRecyclerView,v,position,getItemId(position),path);
        }
        return true;
    }

    public interface OnChildClickListener{
        void onChildClick(RecyclerView parent,View view,int position,long id,String path);
    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public interface OnChildLongClickListener{
        void onChildLongClick(RecyclerView parent,View view,int position,long id,String path);
    }

    public void setOnChildLongClickListener(OnChildLongClickListener onChildLongClickListener) {
        this.onChildLongClickListener = onChildLongClickListener;
    }

    public void addAll(ArrayList<VideoBean> list) {
        videoList.addAll(list);
        notifyDataSetChanged();
    }

    public void add(VideoBean videoBean) {
        videoList.add(videoBean);
        notifyDataSetChanged();
    }

    public void add(int i, VideoBean videoBean) {
        videoList.add(i, videoBean);
        notifyDataSetChanged();
    }

    public void remove(int i){
        videoList.remove(i);
        notifyDataSetChanged();
    }

    public void clear() {
        videoList.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView videoImage;
        private TextView videoName;
        private TextView videoTime;
        public ViewHolder(View itemView) {
            super(itemView);
            videoImage= (ImageView) itemView.findViewById(R.id.video_image);
            videoName= (TextView) itemView.findViewById(R.id.video_name);
            videoTime= (TextView) itemView.findViewById(R.id.video_time);
        }
    }
}
